	package com.lansi.blegateway.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lansi.bean.ResponseResult;
import com.lansi.blegateway.bean.Blegateway;
import com.lansi.blegateway.service.IBlegatewayService;

@Controller
public class BlegatewayController {

	@Resource
	private IBlegatewayService blegatewayService;
	
	//添加蓝牙网关信息
	@RequestMapping("/insertBlegateway.do")
	@ResponseBody
	public ResponseResult<Void> getInsertBlegateway(@RequestBody Map<String,String> map1
			/*String deveui,String alias,String mapid,String x,String y,String alarm,String rang,
			String sn,String offtimebegin,String offtimeend,String type,String memo*/
			) 
			throws JsonParseException, JsonMappingException, IOException, ParseException{
		
		ObjectMapper mapper = new ObjectMapper();    
		@SuppressWarnings("unchecked")
		Map<String,Object> map = mapper.readValue(map1.get("params"), Map.class); 
		
		Object deveui = map.get("deveui");
		Object alias = map.get("alias");
		Object mapid = map.get("mapid");
		Object x = map.get("x");
		Object y = map.get("y");
		Object alarm = map.get("alarm");
		Object rang = map.get("rang");
		Object sn = map.get("sn");
		Object offtimebegin = map.get("offtimebegin");
		Object offtimeend = map.get("offtimeend");
		Object type = map.get("type");
		Object memo = map.get("memo");

		
		
		ResponseResult<Void> rr = new  ResponseResult<Void>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Date timebegin = null;
		Date timeend = null;
		if(offtimebegin != null && !"".equals(offtimebegin.toString().trim())) {
			timebegin = sdf.parse(offtimebegin.toString());
		}
		if(offtimeend != null && !"".equals(offtimeend.toString().trim())) {
			timeend = sdf.parse(offtimeend.toString());
		}
		
		try {
			if((timebegin==null && timeend==null) || timebegin.getTime() < timeend.getTime()) {
		
				Blegateway blegateway = new Blegateway();
				if(deveui != null) blegateway.setDeveui(deveui.toString());
				if(alias != null) blegateway.setAlias(alias.toString());
				if(mapid != null) blegateway.setMapid(mapid.toString());
				if(x == null ||  "".equals(x.toString().trim())) {
					blegateway.setX((float) 0.00);
				}else {
					blegateway.setX(Float.parseFloat(x.toString()));
				}
				if(y == null || "".equals(y.toString().trim())) {
					blegateway.setY((float) 0.00);
				}else {
					blegateway.setY(Float.parseFloat(y.toString()));
				}
				if(alarm != null) blegateway.setAlarm(alarm.toString());
				if(rang != null) blegateway.setRang(rang.toString());
				if(sn != null) blegateway.setSn(sn.toString());
				blegateway.setRecv(new Date());
				if(offtimebegin != null && !"".equals(offtimebegin.toString().trim())) {
					blegateway.setOfftimebegin(timebegin);
				}
				if(offtimeend != null && !"".equals(offtimeend.toString().trim())) {
					blegateway.setOfftimeend(timeend);
				}
				if(type != null) blegateway.setType(type.toString());
				if(memo != null) blegateway.setMemo(memo.toString());
				
				blegatewayService.getInsertBlegateway(blegateway);
				
				rr.setState(1);
				rr.setMessage("蓝牙网关信息添加成功！");
			}else {
				rr.setState(-1);
				rr.setMessage("扫描时间设置不符合要求！");
			}
		} catch (RuntimeException e) {
			
			rr.setState(-1);
			rr.setMessage("添加信息失败！原因："+e.getMessage());
			System.out.println(e.getMessage());
		}
		return rr;
	} 
	
	//删除蓝牙网关信息根据设备号deveui
	@RequestMapping("/delBlegatewayByDeveui.do")
	@ResponseBody
	public ResponseResult<Void> delBlegatewayByDeveui(@RequestBody Map<String,String> map
			){

		String deveui = map.get("deveui");
		
		ResponseResult<Void> rr = new  ResponseResult<Void>();
		try {
			blegatewayService.delBlegatewayByDeveui(deveui);
			
			rr.setState(1);
			rr.setMessage("信息删除成功！");
		} catch (Exception e) {
			rr.setState(-1);
			rr.setMessage("信息删除失败！原因："+e.getMessage());
		}

		return rr;
	}
	
	//修改蓝牙网关信息根据设备号deveui
	@RequestMapping("/updateBlegatewayAllByDeveui.do")
	@ResponseBody
	public ResponseResult<Void> updateBlegatewayAllByDeveui(@RequestBody Map<String,String> map1//,HttpServletRequest request
			
			/*String deveui,String alias,String mapid,String x,String y,String alarm,String rang,
			String sn,String offtimebegin,String offtimeend,String type,String memo*/
			
			) throws JsonParseException, JsonMappingException, IOException, ParseException{
		
		ObjectMapper mapper = new ObjectMapper();    
		@SuppressWarnings("unchecked")
		Map<String,Object> map = mapper.readValue(map1.get("params"), Map.class); 

//		User user = (User)request.getAttribute("user");
//		String prio = user.getPrio();
		String prio = "2";//----------------------------------------------------------------------------------------------------------

		ResponseResult<Void> rr = new  ResponseResult<Void>();
		Blegateway blegateway = new Blegateway();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		try {
				if("1".equals(prio)) {

					Object deveui = map.get("deveui");
					Object alias = map.get("alias");
					Object mapid = map.get("mapid");
					Object x = map.get("x");
					Object y = map.get("y");
					Object alarm = map.get("alarm");
					Object rang = map.get("rang");
					Object sn = map.get("sn");
					Object offtimebegin = map.get("offtimebegin");
					Object offtimeend = map.get("offtimeend");
					Object memo = map.get("memo");
					
					Date timebegin = null;
					Date timeend = null;
					if(offtimebegin != null && !"".equals(offtimebegin.toString().trim())) {
						timebegin = df.parse(offtimebegin.toString());
					}
					if(offtimeend != null && !"".equals(offtimeend.toString().trim())) {
						timeend = df.parse(offtimeend.toString());
					}
					
					if((timebegin==null && timeend==null) || timebegin.getTime() < timeend.getTime()) {
						if(deveui != null) blegateway.setDeveui(deveui.toString());
						if(alias != null) blegateway.setAlias(alias.toString());
						if(mapid != null) blegateway.setMapid(mapid.toString());
						if(x == null || "".equals(x.toString().trim())) {
							blegateway.setX((float) 0.00);
						}else {
							Float a = Float.parseFloat(x.toString());
							System.out.println(a);
							blegateway.setX(Float.parseFloat(x.toString()));
						}
						if(y == null || "".equals(y.toString().trim())) {
							blegateway.setY((float) 0.00);
						}else {
							Float a = Float.parseFloat(y.toString());
							System.out.println(a);
							blegateway.setY(Float.parseFloat(y.toString()));
						}
						if(alarm != null) blegateway.setAlarm(alarm.toString());
						if(rang != null) blegateway.setRang(rang.toString());
						if(sn != null) blegateway.setSn(sn.toString());
						if(offtimebegin != null && !"".equals(offtimebegin.toString().trim())) {
							blegateway.setOfftimebegin(timebegin);
						}
						if(offtimeend != null && !"".equals(offtimeend.toString().trim())) {
							blegateway.setOfftimeend(timeend);
						}
						if(memo != null) blegateway.setMemo(memo.toString());
						blegatewayService.updateBlegatewayAllByDeveui(blegateway);
						
						rr.setState(1);
						rr.setMessage("蓝牙网关信息修改成功(系统管理员)！");
					}else {
						rr.setState(-1);
						rr.setMessage("扫描时间设置不符合要求！");
					}
				}else {
					Object deveui = map.get("deveui");
					Object alias = map.get("alias");
					Object mapid = map.get("mapid");
					Object x = map.get("x");
					Object y = map.get("y");
					Object alarm = map.get("alarm");
					Object rang = map.get("rang");
					Object offtimebegin = map.get("offtimebegin");
					Object offtimeend = map.get("offtimeend");
					
					Date timebegin = null;
					Date timeend = null;
					if(offtimebegin != null && !"".equals(offtimebegin.toString().trim())) {
						timebegin = df.parse(offtimebegin.toString());
					}
					if(offtimeend != null && !"".equals(offtimeend.toString().trim())) {
						timeend = df.parse(offtimeend.toString());
					}
					
					if((timebegin==null && timeend==null) || timebegin.getTime() < timeend.getTime()) {
						
						if(deveui != null) blegateway.setDeveui(deveui.toString());
						if(alias != null) blegateway.setAlias(alias.toString());
						if(mapid != null) blegateway.setMapid(mapid.toString());
						if(x == null || "".equals(x.toString().trim())) {
							blegateway.setX((float) 0.00);
						}else {
							Float a = Float.parseFloat(x.toString());
							System.out.println(a);
							blegateway.setX(Float.parseFloat(x.toString()));
						}
						if(y == null || "".equals(y.toString().trim())) {
							blegateway.setY((float) 0.00);
						}else {
							Float a = Float.parseFloat(y.toString());
							System.out.println(a);
							blegateway.setY(Float.parseFloat(y.toString()));
						}
						if(alarm != null) blegateway.setAlarm(alarm.toString());
						if(rang != null) blegateway.setRang(rang.toString());
						if(offtimebegin != null && !"".equals(offtimebegin.toString().trim())) {
							blegateway.setOfftimebegin(timebegin);
						}
						if(offtimeend != null && !"".equals(offtimeend.toString().trim())) {
							blegateway.setOfftimeend(timeend);
						}
						blegatewayService.updateBlegatewayAllByDeveui(blegateway);
						
						rr.setState(1);
						rr.setMessage("蓝牙网关信息修改成功(项目管理员)！");
					}else {
						rr.setState(0);
						rr.setMessage("扫描时间设置不符合要求！");
					}
				}
			} catch (RuntimeException e) {
				
				rr.setState(-1);
				rr.setMessage("蓝牙网关信息修改失败！");
				System.out.println(e.getMessage());
			}
	
		return rr;
	}
	
	
	//查询蓝牙网关信息根据设备号deveui
	@RequestMapping("/getByDeveui.do")
	@ResponseBody
	public ResponseResult<Blegateway> getByDeveui(String deveui){
		
		ResponseResult<Blegateway> rr = new ResponseResult<Blegateway>();
		
		try {
			Blegateway blegateway = blegatewayService.getByDeveui(deveui);
			rr.setState(1);
			rr.setMessage("获取信息成功！");
			rr.setData(blegateway);
			
		} catch (RuntimeException e) {
			rr.setState(1);
			rr.setMessage("获取信息失败！");
		}
		
		return rr;
	}
	
	//根据username进行分权限分页查询
	@RequestMapping("/getByUsername.do")
	@ResponseBody
	public ResponseResult<Map<String,Object>>  getBlegatewayByUsername(String username,String page	//,HttpServletRequest request
			){
		
//		User user = (User)request.getAttribute("user");
//		String prio = user.getPrio();
//		String sn = user.getSn();
		String sn = "2111";
		String prio = "2";//------------------------------------------------------------------------------------------------------------
		Integer offset = null;
		Integer count = null;
		Integer size = null;
		ResponseResult<Map<String, Object>> rr = new ResponseResult<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			if("1".equals(prio)) {
				count = 10;
				if(page == null) {
					offset = 0;
				}else {
					offset = (Integer.parseInt(page)-1)*10;
				}
				List<Blegateway> list = blegatewayService.getBlegatewayAll(offset, count);
				size = blegatewayService.getSizeBlegatewayAll();
				map.put("list", list);
				map.put("size", size);
				rr.setState(1);
				rr.setMessage("获取所有蓝牙网关信息成功！");
				rr.setData(map);
			}else {
				count = 5;
				if(page == null) {
					offset = 0;
				}else {
					offset = (Integer.parseInt(page)-1)*5;
				}
				List<Blegateway> list = blegatewayService.getBlegatewayBysn(sn, offset, count);
				size = blegatewayService.getSizeBlegatewayBysn(sn);
				map.put("list", list);
				map.put("size", size);
				rr.setState(1);
				rr.setMessage("获取蓝牙网关信息成功！");
				rr.setData(map);
				
			}
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage("获取蓝牙网关信息失败！");
			System.out.println(e.getMessage());
		}
		
		return rr;
		
	}
	
	
	//根据username进行分权限查询所有
		@RequestMapping("/getBlegatewayAllByUsername.do")
		@ResponseBody
		public ResponseResult<Map<String,Object>>  getBlegatewayAllByUsername(String username,String page	//,HttpServletRequest request
				){
			
//			User user = (User)request.getAttribute("user");
//			String prio = user.getPrio();
//			String sn = user.getSn();
			String sn = "2111";
			String prio = "2";//------------------------------------------------------------------------------------------------------------
			Integer offset = null;
			Integer count = null;
			Integer size = null;
			ResponseResult<Map<String, Object>> rr = new ResponseResult<Map<String, Object>>();
			Map<String, Object> map = new HashMap<String, Object>();
			
			try {
				if("1".equals(prio)) {
					count = 10;
					List<Blegateway> list = blegatewayService.getBlegatewayAll(offset, count);
					size = blegatewayService.getSizeBlegatewayAll();
					map.put("list", list);
					map.put("size", size);
					rr.setState(1);
					rr.setMessage("获取所有蓝牙网关信息成功！");
					rr.setData(map);
				}else {
					count = 5;
					List<Blegateway> list = blegatewayService.getBlegatewayBysn(sn, offset, count);
					size = blegatewayService.getSizeBlegatewayBysn(sn);
					map.put("list", list);
					map.put("size", size);
					rr.setState(1);
					rr.setMessage("获取蓝牙网关信息成功！");
					rr.setData(map);
					
				}
			} catch (RuntimeException e) {
				rr.setState(-1);
				rr.setMessage("获取蓝牙网关信息失败！");
				System.out.println(e.getMessage());
			}
			
			return rr;
			
		}
	
	
	//根据username和deveui进行查询
	@RequestMapping("/getBlegatewayByUDeveui.do")
	@ResponseBody
	public ResponseResult<Map<String,Object>>  getBlegatewayByUDeveui(String username,String deveui//,HttpServletRequest request
			){
		
//		User user = (User)request.getAttribute("user");
//		String prio = user.getPrio();
//		String sn = user.getSn();
		String sn = "2111";
		String prio = "2";//----------------------------------------------------------------------------------------------------------------
		Integer count = 10;
		ResponseResult<Map<String, Object>> rr = new ResponseResult<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		List<Blegateway> listutil = new ArrayList<Blegateway>();
		try {
			if("1".equals(prio)) {
				Blegateway blegateway =blegatewayService.getByDeveui(deveui);
				if(blegateway == null) {
					rr.setState(-1);
					rr.setMessage("设备号为"+deveui+"蓝牙网关不存在！");
				}else {
					
					listutil.add(blegateway);
					map.put("list", listutil);
					map.put("size", 1);
					rr.setState(1);
					rr.setMessage("获取信息成功！");
					rr.setData(map);
				}
			}else {
				List<Blegateway> list = blegatewayService.getBlegatewayBysn(sn, null, count);
				Blegateway blegateway =blegatewayService.getByDeveui(deveui);
				if(!list.contains(blegateway) && blegateway != null) {
					rr.setState(-1);
					rr.setMessage("该设备信息您无权限查看！");
				}else if(blegateway == null) {
					rr.setState(-1);
					rr.setMessage("设备号为"+deveui+"蓝牙网关不存在！");
				}else {
					
					listutil.add(blegateway);
					map.put("list", listutil);
					map.put("size", 1);
					rr.setState(1);
					rr.setMessage("获取信息成功！");
					rr.setData(map);
				}
			}
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage("查询信息失败！原因："+e.getMessage());
		}
		
		return rr;
	}
	
	

	
}
